# STAT2 Lecture Slides Section 1.1

Author

Oliver d’Pug

      ### Load the pacman package, or install and load on failure
      if (!require(pacman)){
        install.packages("pacman")
        library(pacman)
      }

    Loading required package: pacman

      p_load(mosaic)

## Fitting a Constant (Mean) Model

The data are in the MidtermFinal.csv file which resides in the Datasets folder. We read this file to create a dataframe.

      MidtermFinal = read.csv("Datasets/MidtermFinal.csv")
      head(MidtermFinal)

       Student Midterm Final
    1  Katelin      35    53
    2  Phoenix      38    68
    3    Maria      50    93
    4     Adam      39    80
    5 Michelle      37    82
    6  Allyssa      46    87

Computing the mean is easy since there is a function mean.

      mean(MidtermFinal$Final)

    [1] 75.3871

      (Final.mean = mean(MidtermFinal$Final))

    [1] 75.3871

We can plot Final as a function of Midterm and add the mean of Final as a horizontal line.

      plot(Final~Midterm, data=MidtermFinal)
      abline(h=Final.mean, col="maroon")

## Fitting a Least Squares Model

Again, the data are in the MidtermFinal.csv file. Since the file was read above, we need only check that the dataframe that was created above exists.

      ls()

    [1] "Final.mean"   "MidtermFinal"

      head(MidtermFinal)

       Student Midterm Final
    1  Katelin      35    53
    2  Phoenix      38    68
    3    Maria      50    93
    4     Adam      39    80
    5 Michelle      37    82
    6  Allyssa      46    87

We see that the dataframe MidtermFinal and the mean of the Final variable, Final.mean, both exist. This can also be seen in the Environment tab at the upper right in RStudio.

We now fit the least squares linear regression model.

      lm(Final~Midterm, data=MidtermFinal)

    Call:
    lm(formula = Final ~ Midterm, data = MidtermFinal)

    Coefficients:
    (Intercept)      Midterm
         18.672        1.493

The model can be saved for later use. The extra parentheses request a display of the result, while **summary* requests more detailed output.

      (Final.lm = lm(Final~Midterm, data=MidtermFinal))

    Call:
    lm(formula = Final ~ Midterm, data = MidtermFinal)

    Coefficients:
    (Intercept)      Midterm
         18.672        1.493

      summary(Final.lm)

    Call:
    lm(formula = Final ~ Midterm, data = MidtermFinal)

    Residuals:
         Min       1Q   Median       3Q      Max
    -18.4621  -6.3571   0.1354   5.7992  16.6279

    Coefficients:
                Estimate Std. Error t value Pr(>|t|)
    (Intercept)  18.6721     9.3311   2.001   0.0548 .
    Midterm       1.4925     0.2413   6.186 9.58e-07 ***
    ---
    Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

    Residual standard error: 9.651 on 29 degrees of freedom
    Multiple R-squared:  0.5689,    Adjusted R-squared:  0.554
    F-statistic: 38.26 on 1 and 29 DF,  p-value: 9.582e-07

A plot with the regression line can be created quite easily.

      plot(Final~Midterm, data=MidtermFinal)
      abline(Final.lm, col="green", lty=2)